package com.xuan.pmh.activity;

import java.util.List;
import java.util.Map;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xuan.pmh.R;
import com.xuan.pmh.bean.Grade;
import com.xuan.pmh.bean.Paper;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ToastUtil;

public class StudentMentalActivity extends HttpActivity {

	private EditText etUserId, etFrom, etTo;
	private Button btFinish, btReturn,btWatchAll;
	private LinearLayout llGrade;

	private Grade grade;
	private List<Grade> grades;

	private final int GET_GRADE = 0;
	private final int GET_GRADES = 1;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		
		switch (requestCode) {
		case GET_GRADE:
			if(gsonBean.state == FinalValue.FAIL){
				ToastUtil.show(this, "请填写正确的学号");
				return;
			}
			grade = gson.fromJson(gsonBean.bean, Grade.class);
			llGrade.removeAllViews();
			llGrade.addView(getView(grade));
			break;
		case GET_GRADES:
			llGrade.removeAllViews();
			grades = gson.fromJson(gsonBean.bean, new TypeToken<List<Grade>>() {
			}.getType());
			for (int i = 0; i < grades.size(); i++) {
				llGrade.addView(getView(grades.get(i)));
			}
			break;
		}

	}

	public View getView(Grade grade) {
		View view = inflater.inflate(R.layout.item_grade_show, null);
		TextView tvUserId, tvName;
		LinearLayout ll;
		tvUserId = (TextView) view.findViewById(R.id.tvUserId);
		tvName = (TextView) view.findViewById(R.id.tvName);
		ll = (LinearLayout) view.findViewById(R.id.llPaperGrade);
		tvUserId.setText("学号:" + grade.student.userId);
		tvName.setText("姓名:" + grade.student.name);
		for (int i = 0; i < grade.papers.size(); i++) {
			Paper paper = grade.papers.get(i);
			View gv = inflater.inflate(R.layout.item_grade, null);
			TextView tvInstructions, tvGrade;
			tvInstructions = (TextView) gv.findViewById(R.id.tvInstructions);
			tvGrade = (TextView) gv.findViewById(R.id.tvGrade);
			tvInstructions.setText(paper.instructions);
			tvGrade.setText(String.valueOf(paper.grade) + "分");
			ll.addView(gv);
		}
		return view;
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_student_mental;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		llGrade = (LinearLayout) findViewById(R.id.llGrade);
		etUserId = (EditText) findViewById(R.id.etUserId);
//		etFrom = (EditText) findViewById(R.id.etFrom);
//		etTo = (EditText) findViewById(R.id.etTo);
		btFinish = (Button) mFindViewAndSetOnClick(R.id.btFinish);
		btReturn = (Button) mFindViewAndSetOnClick(R.id.btReturn);
		btWatchAll = (Button) mFindViewAndSetOnClick(R.id.btWatchAll);
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btReturn:
			finish();
			break;
		case R.id.btFinish:
			if (getEditTextString(etUserId) != null
					&& !getEditTextString(etUserId).equals("")) {
				paramsList.clear();
				paramsList.put("param", getEditTextString(etUserId));
				addPostRequest("getGrade", paramsList, GET_GRADE);
				sendPostRequest();
			} else {
					ToastUtil.show(this, "请填写正确的学号");
			}
			break;
		case R.id.btWatchAll:
			paramsList.clear();
			addPostRequest("getGrades", paramsList, GET_GRADES);
			sendPostRequest();
		}
	}

}
